package com.bilin.coin.fragment.mill;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import com.bilin.coin.R;
import com.bilin.coin.bean.CommonSuccessEvent;
import com.bilin.coin.bean.MillResourceBean;
import com.bilin.coin.common.base.BaseAppCompatActivity;
import com.bilin.coin.common.base.BaseFragment;
import com.bilin.coin.list.MemberList;
import com.bilin.coin.list.MillResourceList;
import com.bilin.coin.tools.AlertUtils;
import com.bilin.coin.tools.Poster;
import com.bilin.coin.utils.DialogUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;

public class MillResourceFragment extends BaseFragment {

    @BindView(R.id.ll_main)
    LinearLayout ll_main;

    MillResourceList digToolsList;

    @Override
    protected void initView(View view, Bundle savedInstanceState) {

        digToolsList = new MillResourceList((BaseAppCompatActivity) mContext);
        //digToolsList.refresh(true);
        ll_main.addView(digToolsList.getRootView(), new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));

        digToolsList.getAdapter().setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                MillResourceBean millResourceBean = (MillResourceBean) adapter.getData().get(position);
                buy(millResourceBean.getName(),millResourceBean.getId());
            }
        });
    }

    private void buy(String name, String id) {
        new Poster((BaseAppCompatActivity) mContext, true, false) {
            @Override
            protected String fillUrl() {
                return "/api/bl-device/buy";
            }

            @Override
            protected Map<String, String> fillParams() {
                HashMap hashMap = new HashMap();
                hashMap.put("id", id);
                return hashMap;
            }

            @Override
            protected void disposeSuccess(String dataJson) {
                super.disposeSuccess(dataJson);
                EventBus.getDefault().post(new CommonSuccessEvent(CommonSuccessEvent.BuyDevicesSuccess));
                digToolsList.refresh(true);

                AlertUtils.dialog((Activity) mContext, "温馨提示", "恭喜您购入一台"+name+"，从次日起开始生效", new AlertUtils.OnPositiveClick() {
                    @Override
                    public void onClick() {

                    }
                });
            }
        };
    }

    @Override
    protected void initData() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.common_layout;
    }

    public void onRefresh() {
        digToolsList.refresh(true);
    }
}
